﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Odbc;

namespace Insight.ComClass
{
 	/// <summary>
	/// ConIST에 대한 요약 설명입니다.
	/// </summary>
	public class InsightConnect
	{
		private static System.Data.Odbc.OdbcConnection istDB;
		private static System.Data.Odbc.OdbcCommand m_OdbcCommand=null;
		private static System.Data.Odbc.OdbcDataReader m_OdbcDataReader=null;

		private static string conString = "DRIVER="+InsightString.DecryptStr(InsightConst.DRIVER)+';'+
										  "SERVER="+InsightString.DecryptStr(InsightConst.SERVER)+';'+
										  "UID="+InsightString.DecryptStr(InsightConst.UID)+';'+
										  "PWD="+InsightString.DecryptStr(InsightConst.PWD);
		private static string istString = InsightString.EncryptStr(conString);


		public static string ISTConnString {get {return istString;} }
			
		public static DataTable fetchData(string _qry)
		{
			DataTable vDt = new DataTable();

			istDB = new OdbcConnection(InsightString.DecryptStr(InsightConnect.istString));
			OdbcDataAdapter ad = new OdbcDataAdapter();
			ad.SelectCommand = new OdbcCommand(_qry, istDB);
			ad.Fill(vDt);			 

			return vDt;
		}

		public static DataSet fetchDataDS(string _qry)
		{
			DataTable vDt = new DataTable();

			istDB = new OdbcConnection(InsightString.DecryptStr(InsightConnect.istString));
			OdbcDataAdapter ad = new OdbcDataAdapter();
			ad.SelectCommand = new OdbcCommand(_qry, istDB);
			DataSet ds = new DataSet("patient");
			ad.Fill(ds);			

			return ds;
		}

		public static DataTable ISTExecuteFill(string qrystr, string maptable)
		{
			istDB = new OdbcConnection(InsightString.DecryptStr(InsightConnect.istString));
			OdbcDataAdapter datadapt = new OdbcDataAdapter();			
			DataSet dataset = new DataSet();
			datadapt.SelectCommand = new OdbcCommand(qrystr, istDB);
			  
			dataset.Tables.Add(maptable);
			try
			{
				datadapt.Fill(dataset.Tables[maptable]);
			}
			catch (SqlException se)
			{
                
				//MessageBox.Show("쿼리가 잘못되었습니다. \n" + se.Message,"오류");
			}		

			DataTable t1 = dataset.Tables[maptable].Copy();			
			return t1;		
		}

		public static void ISTExecuteFill(string qrystr, DataTable aTable)
		{
			if (aTable!=null)
			{				
				istDB = new OdbcConnection(InsightString.DecryptStr(InsightConnect.istString));
				OdbcDataAdapter aOdbcAdapter = new OdbcDataAdapter();
				DataSet dataset = new DataSet();
				aOdbcAdapter.SelectCommand = new OdbcCommand(qrystr, istDB);
			
				aTable.Clear();
				try
				{
					aOdbcAdapter.Fill(aTable);			
				}
				catch (SqlException se)
				{

					//MessageBox.Show("쿼리가 잘못되었습니다. \n" + se.Message,"오류");
				}
			}			
		}

		public static OdbcDataReader ISTExecuteReader(string aQuery)
		{
			istDB = new OdbcConnection(InsightString.DecryptStr(InsightConnect.istString));
			m_OdbcCommand = new OdbcCommand(aQuery, istDB);
			m_OdbcCommand.Connection.Open();
			m_OdbcDataReader = m_OdbcCommand.ExecuteReader();		 	  
					
			return m_OdbcDataReader;		
		}

		public static string ISTExecuteScalar(string aQuery)
		{
			string aValue="";
			
			istDB = new OdbcConnection(InsightString.DecryptStr(InsightConnect.istString));
			m_OdbcCommand = new OdbcCommand(aQuery, istDB);
			m_OdbcCommand.Connection.Open();
			aValue = (string) m_OdbcCommand.ExecuteScalar().ToString();
			m_OdbcCommand.Connection.Close();									
			return aValue;
		}
		
		public static bool ISTExecuteNonQuery(string aQuery)
		{
			bool aResult = false;
			try
			{
				istDB = new OdbcConnection(InsightString.DecryptStr(InsightConnect.istString));
				m_OdbcCommand = new OdbcCommand(aQuery,istDB);
				m_OdbcCommand.CommandText = aQuery;				
				m_OdbcCommand.Connection.Open();
				m_OdbcCommand.ExecuteNonQuery();
				m_OdbcCommand.Connection.Close();
				aResult = true;
			}
			catch
			{
				m_OdbcCommand.Connection.Close();
				aResult = false;
			}
			return aResult;
		}
		
		public static void CloseISTExecuteReader()
		{
			m_OdbcDataReader.Close();
			m_OdbcCommand.Connection.Close();
		}		

		public static bool ISTConnCheck(string aQuery)
		{
			bool aResult = false;
			try
			{
				istDB = new OdbcConnection(InsightString.DecryptStr(InsightConnect.istString));
				m_OdbcCommand.CommandText = aQuery;	
				m_OdbcCommand.Connection.Close();
				aResult = true;
			}
			catch
			{				
				aResult = false;
			}
			return aResult;
		}
    }

    public class Chking
    {
        public static string ChkString(string _str)
        {
            string strTemp = string.Empty;
            strTemp = _str.Replace("'", "''");
            return strTemp;
        }
    }
}